package com.example.workbench.common.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class DecryptUtils {
    private static final String key = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKA0tefycgK8vHDPeXaT4QyC9SsgHOQZ\n" +
            "FhCrAm0z8zw4KmOquzfrZzvE88uudfWRr4cGLJaGHVvE5ttXO9s4KSUCAwEAAQ==";
    public static String decrypt(String encryptedString) throws Exception {
        // 假设这是你的Base64编码的私钥

        // 解码私钥
        byte[] privateKeyDecoded = Base64.getDecoder().decode(key);

        // 构造私钥
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyDecoded);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

        // 使用Cipher解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        // 假设这是你的Base64编码的密文
        String encryptedTextEncoded = "RqKWsRK8xc4RjMgP5LPH+rwKvBmD+pTdUiXpEz2POfHZPepV+cf2zx0KYGg...";

        // 解码密文
        byte[] encryptedTextDecoded = Base64.getDecoder().decode(encryptedTextEncoded);

        // 执行解密
        byte[] decryptedBytes = cipher.doFinal(encryptedTextDecoded);

        // 将解密后的字节转换为字符串
        String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);

        System.out.println("Decrypted Text: " + decryptedText);
        return decryptedText;
    }
    public static void main(String[] args) throws Exception {
        String decrypt = decrypt("ZeFFyXbpqswHdIgXtP0lUA+Cv4/PUupEyjcmfQh2p2HGBFHid8SPGEA2RKxSl/tpcQ2+gWhkIsMfrOWAOULZPw==");

    }
}

